<template>
  <view>
    <!-- 预留出的搜索区域 -->
    <my-search
      @click.native="goToSearch"
      bgcolor="skyblue"
      title="搜你想要的"
      radius="10rpx"
    />
    <view class="scroll-view-container">
      <!-- 左侧的滚动区域 -->
      <scroll-view
        class="left-scroll-view"
        scroll-y
        :style="{ height: wh + 'px' }"
      >
        <view
          class="left-scroll-view-item"
          :class="{ active: index1 === selectedCategory }"
          v-for="(item1, index1) in categoryList"
          :key="index1"
          @click="handleLeftCatClick(index1)"
        >
          {{ item1.cat_name }}
        </view>
      </scroll-view>

      <!-- 右侧的滚动区域 -->
      <scroll-view
        class="right-scroll-view"
        scroll-y
        :style="{ height: wh + 'px' }"
        :scroll-top="scrollTop"
      >
        <view
          class="cate-lv2"
          v-for="(item2, index2) in categoryList[selectedCategory].children"
          :key="index2"
        >
          <view class="cate-lv2-title">/ {{ item2.cat_name }} /</view>
          <!--  -->
          <view class="cate-lv3-list">
            <view
              class="cate-lv3-item"
              v-for="(item3, index3) in item2.children"
              :key="index3"
              @click="goToProductList(item3)"
            >
              <image :src="item3.cat_icon" />
              <text>{{ item3.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import badge from '@/mixins/badge'
export default {
  mixins: [badge],
  data() {
    return {
      // 可用区域的高度
      wh: 0,
      // 分类数组的数据
      categoryList: [],
      // 左侧选中分类的下标
      selectedCategory: 0,
      // 记录右侧页面刷新的滚动
      scrollTop: 0,
    }
  },
  created() {
    this.getWh()
    this.getCategoryList()
  },
  methods: {
    // 获取可用区域的高度
    getWh() {
      const sysinfo = uni.getSystemInfoSync()
      // console.log(sysinfo)
      this.wh = sysinfo.windowHeight - 50
    },
    // 请求分类数据
    async getCategoryList() {
      const { meta, message } = await this.$http.get('/categories')
      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg()
      }
      this.categoryList = message
    },
    // 实现点击一级分类切换选中项效果
    handleLeftCatClick(index) {
      this.selectedCategory = index
      // ! 存在问题 uniapp中 组件中的属性值（如果第一次和第二次的值是一样的，他不会做任何页面更新的操作）
      this.scrollTop = this.scrollTop === 0 ? 1 : 0
    },
    // 点击二级分裂跳转至商品列表
    goToProductList(product) {
      uni.navigateTo({
        url: `/pages_sub1/product-list/product-list?cid=${product.cat_id}`,
      })
    },
    // 搜索跳转至搜索页面
    goToSearch() {
      // console.log('跳跳跳跳跳跳过来了')
      uni.navigateTo({
        url: `/pages_sub1/search/search`,
      })
    },
  },
}
</script>

<style scoped lang="scss">
.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 240rpx;

    .left-scroll-view-item {
      line-height: 120rpx;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 24rpx;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: ' ';
          display: block;
          width: 6rpx;
          height: 60rpx;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }

  .cate-lv2-title {
    font-size: 24rpx;
    font-weight: bold;
    text-align: center;
    padding: 30rpx 0;
  }
  .cate-lv3-list {
    display: flex;
    flex-wrap: wrap;

    .cate-lv3-item {
      width: 33.33%;
      margin-bottom: 20rpx;
      display: flex;
      flex-direction: column;
      align-items: center;

      image {
        width: 120rpx;
        height: 120rpx;
      }

      text {
        font-size: 24rpx;
      }
    }
  }
}
</style>
